#pragma once

#include "../Delegate/Delegate.h"
#include "Invokable.h"
#include "../Threading/AutoLock.h"

#include <cassert>

namespace xgu{

class IAsyncResult;

class MethodInvoker
{
public:
	MethodInvoker(Invokable& invokable, 
		const Delegate<void>& delegate, IAsyncResult** out_async_result)
		: invokable_(&invokable)
	{
		delegate_ = delegate;
		if (out_async_result != NULL)
		{
			assert(!"Not implemented!");
		}
		else
		{
			async_result_ = NULL;
		}
	}

	void Invoke()
	{
		delegate_();
		if (async_result_)
		{
			assert(!"Not implemented!");
		}
		else
		{
			// No need for existence.
			Dispose();
		}
	}
	void Dispose()
	{
		if (invokable_ != NULL)
		{
			invokable_ = NULL;
		}

		delete this;
	}

protected:
	virtual ~MethodInvoker()
	{
		if (async_result_ != NULL)
		{
			assert(!"Not implemented!");
		}
	}

private:

	Invokable* invokable_;
	Delegate<void> delegate_;
	IAsyncResult* async_result_;
};

}
